﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Xml;
using Client.OrderModule;
using DevExpress.XtraBars;

namespace Client
{
    public partial class MainForm : BaseForm
    {
        public MainForm()
        {
            InitializeComponent();
            this.IsMdiContainer = true;
            GlobalInfo.MainForm = this;
            GlobalInfo.UserLookAndFeel = this.LookAndFeel;
            this.bar_Menu.Appearance.BackColor = Color.Transparent;
            this.xtraTabbedMdiManager.PageAdded += new DevExpress.XtraTabbedMdi.MdiTabPageEventHandler(xtraTabbedMdiManager_PageAdded);
            this.xtraTabbedMdiManager.PageRemoved += new DevExpress.XtraTabbedMdi.MdiTabPageEventHandler(xtraTabbedMdiManager_PageRemoved);
            //this.Opacity = 0;
            //this.ShowInTaskbar = false;
           

            //this.xtraTabbedMdiManager.MouseDown += (TabHeadClick);
        }

        void xtraTabbedMdiManager_PageRemoved(object sender, DevExpress.XtraTabbedMdi.MdiTabPageEventArgs e)
        {
            if (this.xtraTabbedMdiManager.Pages.Count == 0)
            {
                this.myPanel1.Visible = true;
            }
            //throw new NotImplementedException();
        }

        void xtraTabbedMdiManager_PageAdded(object sender, DevExpress.XtraTabbedMdi.MdiTabPageEventArgs e)
        {
            this.myPanel1.Visible = false;
            //throw new NotImplementedException();
        }
        //private void TabHeadClick(object sender, MouseEventArgs e)
        //{
        //    if (e.Button != MouseButtons.Right) return;
        //    if (xtraTabbedMdiManager.Pages.Count == 0) return;
        //    this.bbi_CloseAllTabExcept.Enabled = this.xtraTabbedMdiManager.Pages.Count > 1;
        //    Point point = xtraTabbedMdiManager.SelectedPage.TabControl.ScreenPointToControl(Cursor.Position);
        //    foreach (DevExpress.XtraTab.ViewInfo.BaseTabPageViewInfo viewInfo in
        // this.xtraTabbedMdiManager.SelectedPage.TabControl.ViewInfo.HeaderInfo.AllPages)
        //    {
        //        if (viewInfo.Bounds.Contains(point))
        //        {
        //            this.popupMenu_Tabhead.ShowPopup(Cursor.Position);
        //            break;
        //        }
        //    }
        //}



       

        protected override void OnLoad(EventArgs e)
        {
    
            //#if RELEASE
            FormLogOn logOnForm = new FormLogOn();
            if (logOnForm.ShowDialog() == DialogResult.Yes)
            {
                MenuBuilder.GetMenu(this.barManager, this.bar_Menu, "sysmenu.xml");
                this.bsit_user.Caption = "当前用户：" + GlobalInfo.UserLoginInfo.用户ID;

                try
                {
                    WarnInfoReceiver.Instance.Start();

                    
                }
                catch (Exception ex)
                {

                    QunYing.Common.Loger.Log.Error("注册告警监听:" + ex.Message);
                }
                try
                {
                    if (GlobalInfo.UserLoginInfo.角色 == "经销商")
                    {
                        OrderInfoReceiver.Instance.Start();
                    }
                }
                catch (Exception ex)
                {
                    QunYing.Common.Loger.Log.Error("注册订单信息监听:" + ex.Message);
                }

            }
            else
            {
                Application.Exit();
            }
            //#elif DEBUG
            {

                //MenuBuilder.GetMenu(this.barManager, this.bar_Menu, "sysmenu.xml");

            }
           
            //this.Opacity = 1.0;
            //this.ShowInTaskbar = true;
            
          
            //#endif
            base.OnLoad(e);
          
           
        }

        protected override void OnActivated(EventArgs e)
        {
            base.OnActivated(e);
            //FormWelcome fvm = new FormWelcome();
            //fvm.MdiParent = this;
            //fvm.Show();
            //this.ActivedChildForm(fvm);
        }

      

        /// <summary>
        /// 切换子窗体
        /// </summary>
        /// <param name="Childform"></param>
        public void ActivedChildForm(Form Childform)
        {
            if (xtraTabbedMdiManager.Pages[Childform] == null)
            {
                xtraTabbedMdiManager.Pages.Add(new DevExpress.XtraTabbedMdi.XtraMdiTabPage(new DevExpress.XtraTab.XtraTabControl(), Childform));
            }
            if (xtraTabbedMdiManager.SelectedPage == xtraTabbedMdiManager.Pages[Childform]) return;
        
            this.xtraTabbedMdiManager.SelectedPage = xtraTabbedMdiManager.Pages[Childform];
            if (this.xtraTabbedMdiManager.SelectedPage != null)
            {
                this.xtraTabbedMdiManager.SelectedPage.MdiChild.BringToFront();
            }
        }
    }
}
